package cn.wolfcode.controller;


import cn.wolfcode.domain.SystemDictionaryItem;
import cn.wolfcode.qo.SystemDictionaryItemQueryObject;
import cn.wolfcode.service.ISystemDictionaryItemService;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.domain.JsonResult;
import cn.wolfcode.service.ISystemDictionaryService;
import cn.wolfcode.service.impl.SystemDictionaryServiceImpl;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/systemDictionaryItem")
public class SystemDictionaryItemController {

    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;
    @Autowired
    private ISystemDictionaryService systemDictionaryService;


    @RequiresPermissions(value = {"systemDictionaryItem:list","字典明细页面"},logical = Logical.OR)
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") SystemDictionaryItemQueryObject qo){
        if(qo.getParentId()!=-1){
            PageInfo<SystemDictionaryItem> pageInfo = systemDictionaryItemService.query(qo);
            model.addAttribute("pageInfo", pageInfo);
        }
        model.addAttribute("systemDictionarys",systemDictionaryService.listAll());
        return "systemDictionaryItem/list";
    }

    @RequestMapping("/delete")
    @RequiresPermissions(value = {"systemDictionaryItem:delete","字典明细删除"},logical = Logical.OR)
    @ResponseBody
    public JsonResult delete(Long id){
        if (id != null) {
            systemDictionaryItemService.delete(id);
        }
        return new JsonResult();
    }


    @RequestMapping("/saveOrUpdate")
    @RequiresPermissions(value = {"systemDictionaryItem:saveOrUpdate","字典明细新增/编辑"},logical = Logical.OR)
    @ResponseBody
    public JsonResult saveOrUpdate(SystemDictionaryItem systemDictionaryItem){
        if (systemDictionaryItem.getId() != null) {
            systemDictionaryItemService.update(systemDictionaryItem);
        }else {
            //保存的时候自增
            //判断是否填了序号,没有的话自增
            if(systemDictionaryItem.getSequence()==null){
                //获取所属目录
                Long parentId = systemDictionaryItem.getParentId();
                //通过目录查询有多少个序号
                Integer[] sequences = systemDictionaryItemService.selectByParentId(parentId);
                //遍历数组后设置值
                int max = sequences[0];
                for (int i = 0; i < sequences.length; i++) {
                    if(sequences[i]>max){
                        max = sequences[i];
                    }
                }
                max++;
                systemDictionaryItem.setSequence(max);
            }
            systemDictionaryItemService.save(systemDictionaryItem);
        }
        return new JsonResult();
    }
}
